import 'package:flutterclue/utils/JpushUtil.dart';

import 'base_presenter.dart';
import 'package:flutterclue/http_util/constant_url.dart';
import 'package:flutterclue/mvp/mode/login_mode.dart';
import 'package:flutterclue/utils/SharePreferensUtil.dart';

class LoginPresenter extends ClueBasePresenter<LoginView> {
  ///登录
  void login({String account, String password}) async {
    baseView.showLoadingView();
    Map<String, Object> map = new Map();
    map["Account"] = account;
    map["Password"] = password;
    map["CustKey"] = "askdfjkasjofisg5468748646skldjf";
    postData(
            url: ApiUrl.LOGIN_URL,
            params: map,
            error: (error) {
              baseView.dismissLoadingView();
              baseView.onLoginFail(error.toString());
            })
        .then((bean) {
      baseView.dismissLoadingView();
      if (bean.Code == 1) {
        LoginBean loginBean = new LoginBean(bean.Data);

        ///保存用户信息
        SharePreferencesUtil.saveUserInfo(loginBean);
        baseView.onLoginSuccess(loginBean);
      } else {
        SharePreferencesUtil.clearUserInfo();
        baseView.onLoginFail(bean.Message);
      }
    });
  }
  ///保存极光账号到服务器
  void saveJpush() async {
    baseView.showLoadingView();
    Map<String, Object> map = new Map();
    map["AgentId"] =
    await SharePreferencesUtil.getString(SharePreferencesUtil.AgentId);
    map["TopAgentId"] =
    await SharePreferencesUtil.getString(SharePreferencesUtil.TopAgentId);
    String alias = "LXJQRJPushAccount" + await SharePreferencesUtil.getString(SharePreferencesUtil.AgentId);
    map["Account"] = alias;
    postData(
            url: ApiUrl.Save_Jpush_Account,
            params: map,
            error: (error) {

            }).then((bean){
              if(bean.Code==1){
                print("侏儒服务器成功");
                JpushUtil.addTags();
              }else{
                print("侏儒服务器失败");
              }
    });
    }

}


abstract class LoginView extends BaseView {
  void onLoginSuccess(LoginBean loginBean);

  void onLoginFail(String error);
}
